﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace HandmadeShop.Application.Models.BusinessLogic
{
    public class ProductBL
    {
        private HandmadeShop.BusinessLogic.ProductBL bl;

        public ProductBL(HandmadeShop.BusinessLogic.ProductBL productBL)
        {
            this.bl = productBL;
        }

        public List<SelectListItem> GetAllProductType()
        {
            List<SelectListItem> rs = new List<SelectListItem>();

            var list = this.bl.GetAllProductType();

            foreach (var item in list)
            {
                rs.Add(new SelectListItem { Text = item.Name, Value = item.Id.ToString() });
            }

            if (rs.Count > 0)
            {
                rs[0].Selected = true;
            }

            return rs;
        }

        public List<DisplayProductModel> GetNewProducts()
        {
            var rs = new List<DisplayProductModel>();

            var list = this.bl.GetNewProducts();
            if (list == null)
                return rs;

            foreach (var item in list)
            {
                var tmp = this.ConvertToDisplayProductModel(item);

                if (tmp == null) continue;

                rs.Add(tmp);
            }

            return rs;
        }

        public List<DisplayProductModel> GetHotProducts()
        {
            var rs = new List<DisplayProductModel>();

            var list = this.bl.GetHotProducts();
            if (list == null)
                return rs;            

            foreach (var item in list)
            {
                var tmp = this.ConvertToDisplayProductModel(item);

                if (tmp == null) continue;

                rs.Add(tmp);
            }

            return rs;
        }

        public List<DisplayProductModel> GetProductsByType(Guid id)
        {
            var rs = new List<DisplayProductModel>();

            var list = this.bl.GetProductsByType(id);
            if (list == null)
                return rs;

            foreach (var item in list)
            {
                var tmp = this.ConvertToDisplayProductModel(item);

                if (tmp == null) continue;

                rs.Add(tmp);
            }

            return rs;
        }

        public IList<MenuModel> GetMenuItems()
        {
            var rs = new List<MenuModel>();

            var list = this.bl.GetAllProductType();
            if (list == null) 
                return rs;

            foreach (var item in list)
            {
                var tmp = this.ConvertToMenuModel(item);
                if (tmp == null) continue;

                rs.Add(tmp);
            }

            return rs;
        }

        private MenuModel ConvertToMenuModel(HandmadeShop.BusinessLogic.Entities.ProductType productType)
        {
            if (productType == null) return null;

            var rs = new MenuModel
            {
                Id = productType.Id,
                Code = productType.Code,
                Name = productType.Name
            };

            return rs;
        }

        private DisplayProductModel ConvertToDisplayProductModel(HandmadeShop.BusinessLogic.Entities.Product product)
        {
            if (product == null) return null;

            var rs = new DisplayProductModel
            {
                Id = product.Id,
                Code = product.Code,
                Name = product.Name,
                Image1 = product.Image1,
                Image2 = product.Image2,
                Image3 = product.Image3,
                Image4 = product.Image4,
                Image5 = product.Image5,
                Description = product.Description                
            };

            if (product.ProductPrice != null)
            {
                rs.Price = product.ProductPrice.Price.ToString();
            }

            if (product.ProductType != null)
            {
                rs.ProductTypeCode = product.ProductType.Code;
                rs.ProductTypeName = product.ProductType.Name;
            }

            return rs;
        }
    }
}